package fr.alma.trajectory.rendering;

import java.util.Collection;
import java.util.List;

import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLProfile;
import javax.media.opengl.awt.GLCanvas;

import com.jogamp.opengl.util.FPSAnimator;

import fr.alma.trajectory.commons.SpaceObject;
import fr.alma.trajectory.commons.Triangle;

public class Viewer implements IViewer {

	/**
	 * L'animateur d'OpenGL
	 */
	private FPSAnimator animator;
    
	/**
	 * La scene d'OpenGL
	 */
	private GLCanvas scene;
	
	/**
	 * Le rendu d'OpenGL
	 */
	private Renderer renderer;

	// setup OpenGL Version 2
	GLProfile profile = GLProfile.get(GLProfile.GL2);
	GLCapabilities capabilities = new GLCapabilities(profile);

/*	@Override
	public GLCanvas getView(Collection<Triangle> triangles) {
		if(scene == null) {
			scene = new GLCanvas();
			renderer = new Renderer(triangles);
			scene.addGLEventListener(renderer);
			scene.addKeyListener(renderer);
			scene.addMouseListener(renderer);
			scene.addMouseMotionListener(renderer);
			
			animator = new FPSAnimator(scene, 24);
			animator.add(scene);
			this.startAnimation();
		}
		return scene;
	}*/
	
	@Override
	public GLCanvas getView2(Collection<SpaceObject> objets) {
		if(scene == null) {
			scene = new GLCanvas();
			renderer = new Renderer((List<SpaceObject>) objets);
			scene.addGLEventListener(renderer);
			scene.addKeyListener(renderer);
			scene.addMouseListener(renderer);
			scene.addMouseMotionListener(renderer);
			
			animator = new FPSAnimator(scene, 24);
			animator.add(scene);
			this.startAnimation();
		}
		return scene;
	}

	
	@Override
	public boolean setObjectsForScene(Collection<SpaceObject> objets) {
		renderer.setListObjects((List<SpaceObject>) objets);
		return true;
	}
	
	public void startAnimation() {
		animator.start();
	}

	public void stopAnimation() {
		animator.stop();
	}

	public boolean isAnimating() {
		return animator.isAnimating();
	}
	
}
